package com.harmontronics.hanma.paas.paas.controller;

import com.harmontronics.hanma.paas.paas.annotation.TokenValidSkip;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import cn.hutool.core.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmMenuAppService;
import com.harmontronics.hanma.paas.paas.dto.iam.output.HmMenuResult;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmMenuCreateInput;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmMenuUpdateInput;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmMenu;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;

import javax.validation.Valid;


/**
 * 菜单
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-10 10:37:26
 */
@RestController
@RequestMapping("/api/hmmenu")
@Api(tags = {"菜单"})
public class HmMenuController {
    @Autowired
    private HmMenuAppService hmMenuappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmMenuResult>> list() {
        List<HmMenu> hmMenuList = hmMenuappService.getList();

        return ResponseEntity.ok(Convert.toList(HmMenuResult.class, hmMenuList));
    }

    /**
     * 分页列表
     */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmMenuResult>> list(@ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                                       @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmMenu> hmMenuPageBase = hmMenuappService.getList(pageNo, pageSize);
        PageBase<HmMenuResult> resultPageBase = PageBase.copy(hmMenuPageBase, HmMenuResult.class);
        return ResponseEntity.ok(resultPageBase);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmMenuResult> info(@RequestParam("id") Long id) {
        HmMenu hmMenu = hmMenuappService.findById(id);
        HmMenuResult result = BeanUtil.toBean(hmMenu, HmMenuResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<String> save(@Valid @RequestBody HmMenuCreateInput hmMenuInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmMenu hmMenu = BeanUtil.toBean(hmMenuInput, HmMenu.class);
        hmMenuappService.save(hmMenu);

        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody HmMenuUpdateInput hmMenuInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmMenu hmMenu = BeanUtil.toBean(hmMenuInput, HmMenu.class);
        hmMenuappService.update(hmMenu);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmMenuappService.remove(Arrays.asList(ids));

        return ResponseEntity.ok();
    }

}
